// const { default: inquirer } = require("inquirer");
import inquirer from "inquirer";
import axios from "axios";
import dayjs from "dayjs";
// const axios = require("axios");
// const dayjs = require("dayjs");
// const { default: chalk } = require("chalk");
import chalk from "chalk";
import dotenv from "dotenv";
dotenv.config();

const ins = axios.create({
  baseURL: `http://127.0.0.1:${process.env.PORT}`,
});

// 定义问题
const questions = [
  {
    type: "checkbox",
    name: "store",
    message: "请选择门店：",
    choices: [
      {
        name: "福锦店",
        value: "fujin",
      },
      {
        name: "营门口店",
        value: "yingmenkou",
      },
      {
        name: "华润店",
        value: "huarun",
      },
      {
        name: "红光里店",
        value: "hongguangli",
      },
    ],
    validate: (stores) => {
      return stores.length > 0 || "请选择对应的门店！";
    },
  },
  {
    type: "checkbox",
    name: "job",
    message: "请选择任务：",
    choices: [
      {
        name: "OTA数据",
        value: "/runOTA",
      },
      {
        name: "充送数据",
        value: "/runRank",
      },
      {
        name: "区域数据",
        value: "/runArea",
      },
      {
        name: "每日账单",
        value: "/runBill",
      },
      {
        name: "餐饮",
        value: "/runCatering",
      },
    ],
    validate: (jobs) => {
      return jobs.length > 0 || "请选择对应的任务！";
    },
  },
  {
    type: "input",
    name: "startTime",
    message: "请输入开始时间：",
    default: () => {
      return dayjs().subtract(1, "day").format("YYYY-MM-DD");
    },
  },
  {
    type: "input",
    name: "endTime",
    message: "请输入结束时间：",
    default: (anwsers) => {
      return (
        anwsers.startTime || dayjs().subtract(1, "day").format("YYYY-MM-DD")
      );
    },
  },
  {
    type: "confirm",
    name: "confirm",
    message: "是否确认你的选择?",
  },
];

// 发起询问
inquirer.prompt(questions).then(async (answers) => {
  console.log("您选择的门店:", answers.store);
  console.log("您选择的任务:", answers.job);
  console.log("开始时间:", answers.startTime);
  console.log("结束时间:", answers.endTime);
  const jobs = [];
  answers.store.forEach((store) => {
    let diffDays = dayjs(answers.endTime).diff(answers.startTime, "day");
    const paths = [];
    answers.job.forEach((job) => {
      paths.push(job);
    });
    let startTime = answers.startTime;
    let endTime = answers.startTime;
    while (diffDays > -1) {
      diffDays--;
      startTime = dayjs(startTime).format("YYYY-MM-DD 00:00:00");
      endTime = dayjs(endTime).format("YYYY-MM-DD 23:59:59");
      jobs.push({
        params: {
          store,
          startTime,
          endTime,
        },
        paths,
      });
      startTime = endTime = dayjs(startTime).add(1, "day");
    }
  });
  await runJobs(jobs);
});

async function runJobs(jobs) {
  for (const job of jobs) {
    await runJob(job);
  }
}

async function runJob(job) {
  for (const path of job.paths) {
    console.log(
      chalk.blue(
        `开始执行${path}任务，时间是${job.params.startTime}至${job.params.endTime}...`
      )
    );
    await ins.post(path, job.params);
    console.log(
      chalk.green(
        `执行${path}任务，时间是${job.params.startTime}至${job.params.endTime}，执行完成！`
      )
    );
  }
}
